package mathive.client.panels;

import mathive.client.PanelHandler;
import mathive.client.TestService;
import mathive.client.TestServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StatisticsPanel extends VerticalPanel{

	private static final TestServiceAsync SERVICE = GWT.create(TestService.class);
	private VerticalPanel statsPanel = new VerticalPanel();
	private ScrollPanel scrollPanel = new ScrollPanel(statsPanel);
	private Label testName;
	private Button backButton;

	public StatisticsPanel(String name){
		testName = new Label(name);
		backButton = new Button("Back to manage tests");
		backButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clear();
				PanelHandler.showManageTests();
			}
		});
		
		getStatistics(name);
	}
	
	private void getStatistics(String name){

		// Set up the callback object.
		AsyncCallback<String[]> callback = new AsyncCallback<String[]>() {
			public void onFailure(Throwable caught) {
				Window.alert("The server malfunctioned. " +
				"Could not get the statistics.");
			}

			public void onSuccess(String[] stats) {
				clear();
				statsPanel.clear();
				setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
				add(scrollPanel);

//				statsPanel.add(testName);
				for (int i = 0; i < stats.length; i++) {
					statsPanel.add(new Label(stats[i]));
				}
				add(backButton);
			}
		};

		// Make the call to the fetch sudoku service.
		SERVICE.getStatistics(name, callback);

	}

}
